const app = getApp();

Page({
  data: {
    nameValue: app.globalData.userInfo.nickName,
  },
  onShow() {
    this.setData({
      nameValue: app.globalData.userInfo.nickName,
    });
  },
  onSubmit() {
    wx.showLoading({
      title: "正在提交...",
    });
    wx.cloud.callFunction({
      name: "updateUserInfo",
      data: {
        nickName: this.data.nameValue,
      },
      success: (res) => {
        wx.hideLoading();
        const userInfo = res.result;
        console.log("userInfo: ", userInfo);
        // 将用户信息存储在全局变量和缓存中
        app.globalData.userInfo = userInfo;
        wx.setStorageSync("userInfo", userInfo);
        wx.navigateBack({ backRefresh: true });
      },
      fail: (err) => {
        wx.hideLoading();
        console.error("云函数调用失败", err);
      },
    });
  },
  clearContent() {
    this.setData({
      nameValue: "",
    });
  },
});
